package DFS;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author: dust
 * @Date: 2019/11/18 13:39
 */
public class AdjMatrix {
    private int V;
    private int E;
    private int[][] adj;

    public AdjMatrix(String filename) {
        File file=new File(filename);
        try(Scanner scanner=new Scanner(file)){
            V=scanner.nextInt();
            if(V<0)
                throw new IllegalArgumentException("V must be non-negative");
            adj=new int[V][V];
            E=scanner.nextInt();
            if(E<0)
                throw new IllegalArgumentException("E must be non-negative");
            for(int i=0;i<E;i++){
                int a=scanner.nextInt();
                validateVertex(a);
                int b=scanner.nextInt();
                validateVertex(b);

//                判断自环边
                if(a==b)
                    throw new IllegalArgumentException("Self Loop is Detected!");
//                判断平行边
                if(adj[a][b]==1)
                    throw new IllegalArgumentException("Parallel Edges are Detected!");
                adj[a][b]=1;
                adj[b][a]=1;
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private void validateVertex(int v){
        if(v<0||v>=V)
            throw new IllegalArgumentException("vertex"+v+"is invalid");

    }

    public int V(){
        return V;
    }

    public int E(){
        return E;
    }

    public boolean hasEdge(int v,int w){
        validateVertex(v);
        validateVertex(w);
        return adj[v][w]==1;
    }

    public ArrayList<Integer> adj(int v){
        validateVertex(v);
        ArrayList<Integer>res=new ArrayList<>();
        for(int i=0;i<V;i++){
            if(adj[v][i]==1){
                res.add(i);
            }
        }
        return res;
    }

    public int degree(int v){
        validateVertex(v);
        int num=0;
        for(int i=0;i<V;i++){
            if(adj[v][i]==1){
                num++;
            }
        }
        return num;
    }

    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder();
        sb.append(String.format("V=%d,E=%d\n",V,E));
        for(int i=0;i<V;i++){
            for(int j=0;j<V;j++){
                sb.append(String.format("%d ",adj[i][j]));
            }
            sb.append('\n');
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        AdjMatrix adjMatrix=new AdjMatrix("g.txt");
        System.out.println(adjMatrix.toString());
    }

}
